//
//  HotPostCell.m
//  唧歪
//
//  Created by MWJ on 15/7/30.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "HotPostCell.h"

#define audioPostURL @"http://192.168.1.103:8888/index.php/JiWai/audioPlay"  //语音帖子

#define PostdetailsURL @"https://app.jiwaipro.com:8888/index.php/JiWai/postAccording"  //帖子详情

@implementation HotPostCell{
    NSMutableArray *audioPostArray;
    NSMutableArray *audioArray;
    NSString *palyAudioPath;
    NSString *audio_Path;
    UIImageView *img;
    NSMutableArray *postdetailsArray;
}

- (void)awakeFromNib {
    audioPostArray = [NSMutableArray array];
    audioArray = [NSMutableArray array];
    postdetailsArray = [NSMutableArray array];
    [self messageAudio];
    
}

-(void)messageAudio{
    img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 2, 80, 30)];
    _wf = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];
    img.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioplay)];
    [img addGestureRecognizer:singleTap];//点击图片事件
    _wf.image = [UIImage imageNamed:@"音量.png"];
    _lab = [[UILabel alloc]initWithFrame:CGRectMake(img.bounds.size.width-30, 5, 30, 20)];
    _lab.text = @"''";
    _lab.textAlignment = NSTextAlignmentRight;
    img.backgroundColor = [UIColor colorWithRed:252/255.0 green:208/255.0 blue:30/255.0 alpha:1];
    CALayer * layer = [img layer];
    [layer setCornerRadius:8.0];
    layer.borderColor = [[UIColor grayColor] CGColor];
    layer.borderWidth = 1;
    //添加四个边阴影
    img.layer.shadowColor = [UIColor blackColor].CGColor;
    img.layer.shadowOffset = CGSizeMake(0, 0);
    img.layer.shadowOpacity = 0.5;
    img.layer.shadowRadius = 10.0;
    //给iamgeview添加阴影 < wbr > 和边框
    //添加两个边阴影
    img.layer.shadowColor = [UIColor blackColor].CGColor;
    img.layer.shadowOffset = CGSizeMake(4, 4);
    img.layer.shadowOpacity = 0.5;
    img.layer.shadowRadius = 2.0;
    [img addSubview:_wf];
    [img addSubview:_lab];
    [self.audioView addSubview:img];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}
-(void)loadAudioPath:(NSString *)path Str:(NSString *)str{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        audioPostArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshDataAudio:) withObject:audioPostArray waitUntilDone:YES];
    }];
}
-(void)refreshDataAudio:(NSMutableArray *)array{
    for(NSDictionary *dic in array){
        audio_Path = [NSString stringWithFormat:@"%@",dic[@"audio_path"]];
    }
    if (self.avPlay.playing) {
        [self.avPlay stop];
        [self.wf stopAnimating];
        return;
    }else{
        NSURL *url=[NSURL URLWithString:audio_Path];
        NSData *data = [NSData dataWithContentsOfURL:url];
        AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithData:data error:nil];
        self.avPlay = player;
        [self.avPlay prepareToPlay];
        [self.avPlay play];
        self.wf.animationImages = [NSArray arrayWithObjects:
                                           [UIImage imageNamed:@"音量.png"],
                                           [UIImage imageNamed:@"音量2.png"],
//                                           [UIImage imageNamed:@"wifi3.png"],
                                   nil];
        self.wf.animationDuration = 1.0;
        self.wf.animationRepeatCount = 0;
        [self.wf startAnimating];
        self.avPlay.delegate = self;
    }

}

-(void)audioplay{
    NSString *str = [NSString stringWithFormat:@"message_id=%@",self.messageID];
    [self loadAudioPath:audioPostURL Str:str];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    [self.wf stopAnimating];
}

@end
